package com.briup.srms.sms.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.briup.srms.sms.bean.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.briup.srms.sms.bean.StudentEmployment;
import com.briup.srms.sms.bean.StudentEmploymentExample;
import com.briup.srms.sms.bean.extend.StudentEmploymentExtend;
import com.briup.srms.sms.dao.StudentEmploymentMapper;
import com.briup.srms.sms.dao.StudentMapper;
import com.briup.srms.sms.dao.extend.StudentEmploymentExtendMapper;
import com.briup.srms.sms.service.IStudentEmploymentService;
import com.briup.srms.sms.service.IStudentService;
import com.jacky.base.utils.CustomerException;
import com.jacky.base.utils.PageVM;

@Service
public class StudentEmploymentServiceImpl implements IStudentEmploymentService {
	
	@Autowired
	private StudentEmploymentExtendMapper studentEmploymentExtendMapper;
	@Autowired
	private StudentEmploymentMapper studentEmploymentMapper;
	@Autowired
	private IStudentService studentService;
	@Autowired
	private StudentMapper studentMapper;

	@Override
	public PageVM<StudentEmploymentExtend> pageQuery(int page,int pageSize,String realname,Long clazzId,String status,Long[] channelIds,String[] stuTypes,String year,String employmentTeacher,String employmentClazzName) {
		List<StudentEmploymentExtend> list = studentEmploymentExtendMapper.query(page, pageSize, realname, clazzId, status, channelIds, stuTypes,year, employmentTeacher,employmentClazzName);
		long total = studentEmploymentExtendMapper.count(realname, clazzId, status, channelIds, stuTypes,year,employmentTeacher,employmentClazzName);
		return new PageVM<>(page, pageSize, total, list);
	}

	@Override
	public void update(StudentEmployment employment) {

		// 1. 判断学生表中 学生信息是否存在
		Student student = studentService.findByIdCard(employment.getIdCard());
		if (student == null) {
			throw new CustomerException("学生信息不存在");
		}
		// 2. 判断学生就业表中 学生信息是否存在
		// 根据身份证查询
		StudentEmploymentExample example = new StudentEmploymentExample();
		example.createCriteria().andIdCardEqualTo(employment.getIdCard());
		List<StudentEmployment> list = studentEmploymentMapper.selectByExample(example);
		if(list.size()>0) {
			StudentEmployment se = list.get(0);
			if(employment.getId() == null){
				employment.setId(se.getId());
			}
			// 如果数据已经存在执行更新操作
			studentEmploymentMapper.updateByPrimaryKeySelective(employment);
		} else {
			// 如果数据不存在，执行操作数据
			studentEmploymentMapper.insert(employment);
			student.setEmploymentId(employment.getId());
			studentMapper.updateByPrimaryKey(student);
		}
	}

	@Override
	@Transactional
	public List<StudentEmployment> batchUpdate(List<StudentEmployment> employments) {
		List<StudentEmployment> list = new ArrayList<>();
		for (StudentEmployment employment : employments) {
			// 1. 判断学生表中 学生信息是否存在
			Student student = studentService.findByIdCard(employment.getIdCard());
			if (student == null) {
				list.add(employment);
				continue;
			}
			this.update(employment);
		}
		return list;
	}

}
